﻿using System;
using System.Xml;
using System.Web;
using System.Reflection;
using System.Collections.Generic;

using DreamCube.Foundation.Basic.Utility;
using DreamCube.Foundation.Serialization;

namespace DreamCube.Websites.UI.Common.Agents
{
    /// <summary>
    /// 执行服务器方法
    /// </summary>
    public class ExecServerFunction : IHttpHandler, System.Web.SessionState.IReadOnlySessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                context.Response.ContentType = "text/plain";
                String inputStr = MyWeb.GetPostString();
                if (!String.IsNullOrEmpty(inputStr))
                {
                    XmlDocument doc = MyXml.CreateDocByXML(inputStr);
                    String funName = MyXml.GetInnerText(doc, "/Exec/FunName");
                    String assemblyName = MyXml.GetInnerText(doc, "/Exec/AssemblyName");
                    String dataType = MyXml.GetInnerText(doc, "/Exec/DataType");
                    XmlNodeList paramNodeList = doc.SelectNodes("/Exec/Parameters/Parameter");

                    List<Object> valueList = new List<Object>();
                    if (paramNodeList != null && paramNodeList.Count > 0)
                    {
                        for (Int32 i = 0; i < paramNodeList.Count; i++)
                        {
                            String value = MyXml.GetInnerText((XmlElement)paramNodeList[i], "./Value");
                            if (String.IsNullOrEmpty(value)) valueList.Add(null);
                            else valueList.Add(value);
                        }
                    }
                    Object result = MyReflection.ExecFunction(funName, assemblyName, valueList.ToArray());
                    switch (dataType)
                    {
                        case "text":
                            if (result == null)
                                context.Response.Write("");
                            else
                                context.Response.Write(result.ToString());
                            break;
                        case "json":
                            if (result == null)
                                context.Response.Write("");
                            else
                                context.Response.Write(MyJson.Serialize(result));
                            break;
                        case "xml":
                            //context.Response.Write();
                            break;
                    }
                }
                else
                {
                    throw new Exception("传入的调用参数不能为空");
                }
            }
            catch (Exception ex)
            {
                MyLog.MakeLog(ex);
                context.Response.Write("");
            }
            finally
            {
                context.Response.End();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}